<template>
  <div class="cart">
    <div class="product">
      <template v-for="item,index in productList" :key="index">
        <div class="product__item" v-if="item.count>0">
          <i class="product__item__checked iconfont"
          v-html="item.check ? '&#xe70f;': '&#xe603;'"></i>
          <img :src="item.imgUrl" class="product__item__img">
          <div class="product__item__detail">
            <h4 class="product__item__title">{{item.name}}</h4>
            <p class="product__item__price">
              <span class="product__item__yen">&yen;</span>{{item.price}}
              <span class="product__item__origin">&yen;{{item.oldPrice}}</span>
            </p>
          </div>
          <div class="product__number">
            <span class="product__number__minus"
            @click="changeCartItemInfo(shopId,item._id,item,-1)">-</span>
              <!-- {{cartList?.[shopId]?.[item._id]?.count || 0}} -->
              {{item.count || 0}}
            <span class="product__number__plus"
            @click="changeCartItemInfo(shopId,item._id,item,1)">+</span>
          </div>
        </div>
      </template>
    </div>
    <div class="check">
      <div class="check__icon">
        <img
          src="http://www.dell-lee.com/imgs/vue3/basket.png"
          class="check__icon__img"
        />
        <div class="check__icon__tag">{{total}}</div>
      </div>
      <div class="check__info">
        总计：<span class="check__info__price">&yen; {{price}}</span>
      </div>
      <div class="check__btn">去结算</div>
    </div>
  </div>
</template>

<script>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { useCommonCartEffect } from './commonCartEffect'
// 购物车相关信息
const useCartEffect = () => {
  const { changeCartItemInfo } = useCommonCartEffect()
  const store = useStore()// 获取vuex数据
  const route = useRoute()// 获取路由id
  const shopId = route.params.id// 获取id
  const cartList = store.state.cartList// 获取cartList
  // 计算总数
  const total = computed(() => { // 计算属性
    const productList = cartList[shopId]// 当前id下的cartList
    let count = 0
    if (productList) {
      for (const i in productList) { // 遍历当前id下的cartList
        const product = productList[i]
        count += product.count
      }
    }
    return count
  })
  // 计算总金额
  const price = computed(() => { // 计算属性
    const productList = cartList[shopId]// 当前id下的cartList
    let count = 0
    if (productList) {
      for (const i in productList) { // 遍历当前id下的cartList
        const product = productList[i]
        if (product.check) { //* 如果勾选上了就计算金额
          count += (product.count * product.price)
        }
      }
    }
    return count.toFixed(2)// 四舍五入两位
  })
  // 渲染展示购物车
  const productList = computed(() => {
    const productList = cartList[shopId] || []// 如果不存在就是空
    return productList
  })
  return { total, price, productList, shopId, changeCartItemInfo }
}

export default {
  setup () {
    const { total, price, productList, shopId, changeCartItemInfo } = useCartEffect()
    return { total, price, productList, changeCartItemInfo, shopId }// 这里有个bug:shopID如果不导出去会出现+5减1时变成0的错误
  }
}
</script>

<style lang="scss" scoped>
@import '../../style/viriables.scss';
@import '../../style/mixins';
.cart {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
}
.product {
  overflow-y: scroll;
  flex: 1;
  background: #fff;
  &__item {
    position: relative;
    display: flex;
    padding: .12rem 0;
    margin: 0 .16rem;
    border-bottom: .01rem solid $content-bgColor;
    &__checked{
      color: #0091ff;
      margin-right: .2rem;
      line-height: .5rem;
      font-size: .2rem;
    }
    &__detail {
      overflow: hidden;
    }
    &__img {
      width: .46rem;
      height: .46rem;
      margin-right: .16rem;
    }
    &__title {
      margin: 0;
      line-height: .2rem;
      font-size: .14rem;
      color: $content-fontcolor;
      @include ellipsis
    }
    &__price {
      margin: .06rem 0 0;
      line-height: .2rem;
      font-size: .14rem;
      color: $hightlight-fontColor;
    }
    &__yen {
      font-size: .12rem;
    }
    &__origin {
      margin-left: .06rem;
      line-height: .2rem;
      font-size: .12rem;
      color: $light-fontColor;
      text-decoration: line-through;
    }
    .product__number {
      position: absolute;
      right: 0;
      bottom: .12rem;
      &__minus, &__plus
       {
        display: inline-block;
        width: .2rem;
        height: .2rem;
        line-height: .16rem;;
        border-radius: 50%;
        font-size: .2rem;
        text-align: center;
      }
      &__minus {
        border: .01rem solid $medium-fontColor;
        color: $medium-fontColor;
        margin-right: .05rem;
      }
      &__plus {
        background: $btn-bgColor;
        color: $bgColor;
        margin-left: .05rem;
      }
    }
  }
}
.check {
  display: flex;
  height: .49rem;
  border-top: .01rem solid $content-bgColor;
  line-height: .49rem;
  &__icon {
    position: relative;
    width: .84rem;
    &__img {
      display: block;
      margin: .12rem auto;
      width: .28rem;
      height: .26rem;
    }
    &__tag {
      position: absolute;
      right: .2rem;
      top: .04rem;
      width: .2rem;
      height: .2rem;
      line-height: .2rem;
      background-color: $hightlight-fontColor;
      border-radius: 50%;
      font-size: .12rem;
      text-align: center;
      color: #fff;
      transform: scale(.5);
    }
  }
  &__info {
    flex: 1;
    color: $content-fontcolor;
    font-size: .12rem;
    &__price {
      line-height: .49rem;
      color: $hightlight-fontColor;
      font-size: .18rem;
    }
  }
  &__btn {
    width: .98rem;
    background-color: #4FB0F9;
    text-align: center;
    color: #FFF;
    font-size: .14rem;
  }
}
</style>
